exports.GAManifestContentDownloader = Core.Class.subclass({

    classname: 'GAManifestContentDownloader',

    initialize: function(url, localPath, filename, ref) {
		this._url = url;
		this._localPath = localPath;
        this._filename = filename;
        this._reference = ref;
         
        this.doDownload();
    },
    doDownload: function() {
        this.dl = new Network.DownloadManifest(); 
        this.dl.start(this._url, this._localPath, this._filename, this._progressCb.bind(this._reference), this._doneCb.bind(this));
    },
    retry: function() {
    	this.dl.start(this._url, this._localPath, this._filename, this._progressCb.bind(this._reference), this._doneCb.bind(this));
    },
    _progressCb: function ( completedRequests, totalRequests ) {
		if(completedRequests <= totalRequests){
			var percentage = 0;
			if(totalRequests > 0) {
				percentage = (parseInt((completedRequests/totalRequests)*100))*0.25;
			}
			else {
				percentage = 25;
			}
			this.updateDownloadingProgress(percentage);
		}
		else {
			
		}
	},
    _doneCb: function ( err, manifest ) {
        if(err) {
        	var errorAlert = new UI.AlertDialog();
    		errorAlert.setTitle(ALERT_MSG_FAILURE_TITLE);
    		errorAlert.setText(ALERT_MSG_MANIFEST_DOWNLOAD_FAILURE);
    		errorAlert.setChoices(["Retry"]);
			var self = this;
			errorAlert.onchoice = function(ret) {
				errorAlert.hide();
				self.retry();
				errorAlert.destroy();
			};
			errorAlert.show();
        }	
        else {
        	this._reference.doneDownloading();
        }
    },
    destroy: function() {
		if(this.dl)
		{
			this.dl.destroy();
			this.dl = null;
		}
    }
});
